package com.ganbo.rabbitmq.basic;

import com.ganbo.rabbitmq.dto.MailConstants;
import com.ganbo.rabbitmq.dto.User;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 基本消息发送
 *
 * @Author: ganbo
 * @Date: 2020/12/8 19:21
 */

@RestController
@RequestMapping("/basic")
public class BasicProducerController {


    @Autowired
    RabbitTemplate rabbitTemplate;

    ExecutorService executorService = Executors.newFixedThreadPool(5);

    @GetMapping("/sendBasicMsg")
    public String sendDirectMessage() {


        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {
            int finalI = i;
            executorService.submit(() -> {
                String messageData = "test message, hello!";
                String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                Map<String, Object> map = new HashMap<>();
                map.put("messageId", finalI);
                map.put("messageData", messageData);
                map.put("createTime", createTime);
//            rabbitTemplate.convertAndSend(BasicRabbitConfig.EXCHANGE_BASIC_FANOUT, BasicRabbitConfig.ROUTING_KEY_BASIC, map);

                String exchange = BasicRabbitConfig.EXCHANGE_BASIC_FANOUT;
                if (finalI == 99) {
                    exchange = "2";
                }

                rabbitTemplate.convertAndSend(exchange, "*", map);
                System.out.println(Thread.currentThread().getId() + "发送消息条数======>" + finalI);
            });
        }

        System.out.println("主线程执行完毕================>" + (System.currentTimeMillis() - startTime));
        return "ok";
    }



}
